<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="Never stop learning">
<meta property="og:type" content="website">
<meta property="og:title" content="Xiaolong&#39;s Blog">
<meta property="og:url" content="http://example.com/page/4/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="Never stop learning">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Xiaolong">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/page/4/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"en","comments":"","permalink":"","path":"page/4/index.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/05/59/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/09/05/59/" class="post-title-link" itemprop="url">JS数组</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-09-05 12:28:59" itemprop="dateCreated datePublished" datetime="2021-09-05T12:28:59+08:00">2021-09-05</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="数组对象"><a href="#数组对象" class="headerlink" title="数组对象"></a>数组对象</h2><p><code>Array</code> 对象是用于构造数组的全局<strong>对象</strong>。</p>
<h3 id="创建数组"><a href="#创建数组" class="headerlink" title="创建数组"></a>创建数组</h3><h4 id="新建"><a href="#新建" class="headerlink" title="新建"></a>新建</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">let</span> arr <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="keyword">let</span> arr = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">3</span>) <span class="comment">// 长度</span></span><br></pre></td></tr></table></figure>

<h4 id="转化"><a href="#转化" class="headerlink" title="转化"></a>转化</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = <span class="string">&#x27;1, 2, 3&#x27;</span></span><br><span class="line">arr1.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>) <span class="comment">// 将字符串用逗号隔开，转化为数组</span></span><br><span class="line"><span class="keyword">let</span> arr2 = <span class="string">&#x27;123&#x27;</span></span><br><span class="line">arr2.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>) <span class="comment">// 或用空字符串</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 示例</span></span><br><span class="line"><span class="title class_">Array</span>.<span class="title function_">from</span>(<span class="string">&#x27;123&#x27;</span>)</span><br><span class="line">[<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>]</span><br><span class="line"><span class="title class_">Array</span>.<span class="title function_">from</span>(&#123;<span class="number">0</span>:<span class="string">&#x27;a&#x27;</span>,<span class="number">1</span>:<span class="string">&#x27;b&#x27;</span>,<span class="number">2</span>:<span class="string">&#x27;c&#x27;</span>,<span class="number">3</span>:<span class="string">&#x27;d&#x27;</span>,<span class="attr">length</span>:<span class="number">4</span>&#125;)</span><br><span class="line">[ <span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span> ]</span><br><span class="line"><span class="title class_">Array</span>.<span class="title function_">from</span>(&#123;<span class="number">0</span>:<span class="string">&#x27;a&#x27;</span>,<span class="number">1</span>:<span class="string">&#x27;b&#x27;</span>,<span class="number">2</span>:<span class="string">&#x27;c&#x27;</span>,<span class="number">3</span>:<span class="string">&#x27;d&#x27;</span>,<span class="attr">length</span>:<span class="number">2</span>&#125;)</span><br><span class="line">[ <span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>]</span><br></pre></td></tr></table></figure>

<h4 id="伪数组"><a href="#伪数组" class="headerlink" title="伪数组"></a>伪数组</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 伪数组的原型链中没有数组的原型</span></span><br><span class="line">array1 = &#123;<span class="number">0</span>:<span class="string">&#x27;a&#x27;</span>,<span class="number">1</span>:<span class="string">&#x27;b&#x27;</span>,<span class="number">2</span>:<span class="string">&#x27;c&#x27;</span>,<span class="attr">length</span>:<span class="number">3</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> divList = <span class="variable language_">document</span>.<span class="title function_">querySelectorAll</span>(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line"><span class="comment">// 获取的数组是 伪数组</span></span><br><span class="line"><span class="keyword">let</span> divArray = <span class="title class_">Array</span>.<span class="title function_">from</span>(divList)</span><br><span class="line"><span class="comment">// 转化为数组</span></span><br></pre></td></tr></table></figure>

<h4 id="续"><a href="#续" class="headerlink" title="续"></a>续</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 合并两个数组，得到新数组</span></span><br><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>]</span><br><span class="line"><span class="keyword">let</span> arr2 = [<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="keyword">let</span> arr3 = arr1.<span class="title function_">concat</span>(arr2)</span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="comment">// 截取一个数组的一部分</span></span><br><span class="line">arr3.<span class="title function_">slice</span>(<span class="number">1</span>) <span class="comment">// 从第二个元素开始</span></span><br><span class="line"><span class="keyword">let</span> arr4 = arr3.<span class="title function_">slice</span>(<span class="number">2</span>)</span><br><span class="line">[<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"></span><br><span class="line">arr5 = arr3.<span class="title function_">alice</span>(<span class="number">0</span>)</span><br><span class="line"><span class="comment">// 相当于复制数组</span></span><br><span class="line"><span class="comment">// 注意 JS 只提供浅拷贝</span></span><br></pre></td></tr></table></figure>



<h3 id="删除数组"><a href="#删除数组" class="headerlink" title="删除数组"></a>删除数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>]</span><br><span class="line"><span class="keyword">delete</span> arr[<span class="number">0</span>]</span><br><span class="line">arr <span class="comment">// [empty,&#x27;b&#x27;,&#x27;c&#x27;]</span></span><br><span class="line"><span class="comment">// 长度没变</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 修改 length</span></span><br><span class="line">arr.<span class="property">length</span> = <span class="number">1</span></span><br><span class="line">arr <span class="comment">//[&#x27;a&#x27;]</span></span><br><span class="line"><span class="comment">// 不要随便修改 length</span></span><br></pre></td></tr></table></figure>

<h4 id="删除头部元素"><a href="#删除头部元素" class="headerlink" title="删除头部元素"></a>删除头部元素</h4><p><code>arr.shift()</code> arr 被修改，并返回被删元素</p>
<h4 id="删除尾部元素"><a href="#删除尾部元素" class="headerlink" title="删除尾部元素"></a>删除尾部元素</h4><p><code>arr.pop()</code> arr 被修改，并返回被删元素</p>
<h4 id="删除中间元素"><a href="#删除中间元素" class="headerlink" title="删除中间元素"></a>删除中间元素</h4><p><code>arr.splice(index, 1)</code> 删除 index 的一个元素</p>
<p><code>arr.splice(index, 1, ’x’)</code> 并在删除位置添加 <code>‘x’</code></p>
<p><code>arr.splice(index, 1, ’x’, ’y’)</code> 并在删除位置添加 <code>‘x’, &#39;y&#39;</code></p>
<h3 id="查看所有元素"><a href="#查看所有元素" class="headerlink" title="查看所有元素"></a>查看所有元素</h3><h4 id="查看所有属性名"><a href="#查看所有属性名" class="headerlink" title="查看所有属性名"></a>查看所有属性名</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">arr.<span class="property">x</span> = <span class="string">&#x27;x&#x27;</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">keys</span>(arr)</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">values</span>(arr)</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> key <span class="keyword">in</span> arr) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;$&#123;keys&#125;: $&#123;arr[key]&#125;&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="查看数字（字符串）属性名和值"><a href="#查看数字（字符串）属性名和值" class="headerlink" title="查看数字（字符串）属性名和值"></a>查看数字（字符串）属性名和值</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`<span class="subst">$&#123;i&#125;</span>: <span class="subst">$&#123;[i]&#125;</span>`</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">arr.<span class="title function_">forEach</span>(<span class="keyword">function</span>(<span class="params">item, index</span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`<span class="subst">$&#123;index&#125;</span>: <span class="subst">$&#123;item&#125;</span>`</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="forEach原理"><a href="#forEach原理" class="headerlink" title="forEach原理"></a><strong>forEach原理</strong></h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">forEach</span>(<span class="params">array,fn</span>)&#123;</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; array.<span class="property">length</span>; i++)&#123;</span><br><span class="line">    <span class="title function_">fn</span>(array[i], i, array); </span><br><span class="line">    <span class="comment">// 获取数组每一项，把数值传到 fn</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">forEach</span>([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>], <span class="keyword">function</span>(<span class="params">x, y, z</span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(x, y, z);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// forEach 用 for 访问 array 的每一项</span></span><br><span class="line"><span class="comment">// 对每一项调用 fn(array[i], i, array)</span></span><br></pre></td></tr></table></figure>



<h3 id="查看单个属性"><a href="#查看单个属性" class="headerlink" title="查看单个属性"></a>查看单个属性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>]arr[<span class="number">0</span>]</span><br></pre></td></tr></table></figure>

<h4 id="索引越界"><a href="#索引越界" class="headerlink" title="索引越界"></a>索引越界</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arr[arr.<span class="property">legnth</span>] === undefinedarr[-<span class="number">1</span>] === <span class="literal">undefined</span><span class="comment">// 示例let arr = [1,2,3,4,5,6,7]for(let i = 0; i&lt;= arr.length; i++)&#123;    console.log(arr[i].toString());&#125;// 报错: Cannot read property &#x27;toString&#x27; of undefined// 意思是你读取了 undefined 的 toString 属性// 不是 toString 的 undefined// x.toString() 其中 x 如果是 undefined 就报这个错</span></span><br></pre></td></tr></table></figure>

<h4 id="查找某个元素是否在数组里-indexOf"><a href="#查找某个元素是否在数组里-indexOf" class="headerlink" title="查找某个元素是否在数组里 indexOf"></a>查找某个元素是否在数组里 indexOf</h4><p><code>arr.indexOf(item)</code> 存在换回索引,否则返回 -1</p>
<h4 id="使用条件查找元素-find"><a href="#使用条件查找元素-find" class="headerlink" title="使用条件查找元素 find"></a>使用条件查找元素 find</h4><p><code>arr.find(item =&gt; item % 2 === 0)</code> 找第一个偶数</p>
<h4 id="使用条件查找元素的索引-findIndex"><a href="#使用条件查找元素的索引-findIndex" class="headerlink" title="使用条件查找元素的索引 findIndex"></a>使用条件查找元素的索引 findIndex</h4><p><code>arr.findIndex(item =&gt; item % 2 === 0)</code> 找第一个偶数的索引</p>
<h3 id="增加数组中的元素"><a href="#增加数组中的元素" class="headerlink" title="增加数组中的元素"></a>增加数组中的元素</h3><h4 id="在尾部添加元素-push"><a href="#在尾部添加元素-push" class="headerlink" title="在尾部添加元素 push"></a>在尾部添加元素 push</h4><p><code>arr.push(newItem)</code> 修改数组，返回新长度</p>
<p><code>arr.push(item1, item2)</code> 修改数组，返回新长度</p>
<h4 id="在头部添加元素-unshitf"><a href="#在头部添加元素-unshitf" class="headerlink" title="在头部添加元素 unshitf"></a>在头部添加元素 unshitf</h4><p><code>arr.unshift(newItem)</code> 修改数组，返回新长度</p>
<p><code>arr.unshift(item1, item2)</code> 修改数组，返回新长度</p>
<h4 id="在中间添加元素-splice"><a href="#在中间添加元素-splice" class="headerlink" title="在中间添加元素 splice"></a>在中间添加元素 splice</h4><p><code>arr.splice(index, 0, ‘x’)</code> 在 index 出插入 <code>’x’</code></p>
<p><code>arr.splice(index. 0, ‘x’, ‘y’)</code> 插入 <code>‘X’,’y’</code></p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">Array.prototypr.splice() MDN</a></p>
<h3 id="修改数组中的元素"><a href="#修改数组中的元素" class="headerlink" title="修改数组中的元素"></a>修改数组中的元素</h3><h4 id="反转顺序-reverse"><a href="#反转顺序-reverse" class="headerlink" title="反转顺序 reverse"></a>反转顺序 reverse</h4><p><code>arr.reverse()</code>  修改原数组</p>
<h4 id="自定义顺序-sort"><a href="#自定义顺序-sort" class="headerlink" title="自定义顺序 sort"></a>自定义顺序 sort</h4><p><code>arr.sort((a, b)) =&gt; a-b)</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 示例let arr = [1,4,2,3,5]arr.sort() // [1,2,3,4,5]arr.sort(function(a, b)&#123;    console.log(&#x27;a:&#x27;+ a)    console.log(&#x27;b:&#x27;+ b)    if(a&gt;b)&#123;        return 1    &#125;else if(a===b)&#123;        return 0    &#125;else&#123;        return -1    &#125;&#125;)// 等价于arr.sort((a, b) =&gt; a-b) </span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [    &#123;<span class="attr">name</span>: <span class="string">&#x27;jack&#x27;</span>, <span class="attr">score</span>: <span class="number">99</span>&#125;,    &#123;<span class="attr">name</span>: <span class="string">&#x27;erik&#x27;</span>, <span class="attr">score</span>: <span class="number">96</span>&#125;,    &#123;<span class="attr">name</span>: <span class="string">&#x27;evan&#x27;</span>, <span class="attr">score</span>: <span class="number">100</span>&#125;]arr1.<span class="title function_">sort</span>(<span class="keyword">function</span>(<span class="params">a, b</span>)&#123;    <span class="keyword">if</span>(a.<span class="property">score</span> &gt; b.<span class="property">score</span>)&#123;<span class="keyword">return</span> <span class="number">1</span>&#125;    <span class="keyword">else</span> <span class="keyword">if</span>(a.<span class="property">score</span> === b.<span class="property">score</span>)&#123;<span class="keyword">return</span> <span class="number">0</span>&#125;    <span class="keyword">else</span>&#123;<span class="keyword">return</span> -<span class="number">1</span>&#125;&#125;)<span class="comment">// 等价于arr1.sort((a, b) =&gt; a-b)</span></span><br></pre></td></tr></table></figure>



<p>反转字符串</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s = <span class="string">&#x27;abcdefg&#x27;</span>;s.<span class="title function_">plit</span>(<span class="string">&#x27;&#x27;</span>).<span class="title function_">reverse</span>().<span class="title function_">join</span>(<span class="string">&#x27;&#x27;</span>)</span><br></pre></td></tr></table></figure>



<h3 id="数组变换"><a href="#数组变换" class="headerlink" title="数组变换"></a>数组变换</h3><center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-09-01_19-41-33.png" alt="Snipaste_2021-09-01_19-41-33" width="800px"></center>



<h4 id="map"><a href="#map" class="headerlink" title="map"></a>map</h4><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Array.prototype.map() MDN</a></p>
<p>map() 方法<strong>返回一个新数组</strong>，数组中的元素为原始数组元素调用函数处理后的值。</p>
<p>map() 方法按照原数组元素顺序依次处理元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]<span class="comment">// 将数组每个值平方arr1.map((item) =&gt; (item * item))// 等价于for(let i = 0; i &lt; arr1.length; i++)&#123;    arr1[i] = arr1[i] * arr1[i]&#125;</span></span><br></pre></td></tr></table></figure>



<h4 id="filter"><a href="#filter" class="headerlink" title="filter"></a>filter</h4><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">Array.prototype.filter() MDN</a> </p>
<p>filter() 方法<strong>返回一个新的数组</strong>，新数组中的元素是通过检查指定数组中符合条件的所有元素。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]<span class="comment">// 偶数arr1.filter(item =&gt; item % 2 === 0)// 返回数组元素长度大于6let words = [&#x27;spray&#x27;, &#x27;limit&#x27;, &#x27;elite&#x27;, &#x27;exuberant&#x27;, &#x27;destruction&#x27;, &#x27;present&#x27;];words.filter(word =&gt; word.length &gt; 6)</span></span><br></pre></td></tr></table></figure>



<h4 id="reduce"><a href="#reduce" class="headerlink" title="reduce"></a>reduce</h4><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce">Array.prototype.reduce.() MDN</a></p>
<p>reduce() 方法对数组中的每个元素执行一个由您提供的 reducer 函数(升序执行)，将其结果汇总为<strong>单个返回值</strong>。</p>
<p>reduce() 可以作为一个高阶函数，用于函数的 compose。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]<span class="comment">// 累加let sum = 0;for(let i = 0; i &lt; arr.length; i++)&#123;    sum += arr[i]&#125; consloe.log(sum)// 等价于arr.reduce((sum, item) =&gt; sum + item, 0)// 从5开始：5 + 1 + 2 + 3 + 4 + 5 + 6 = 26arr.reduce((sum, item) =&gt; sum + item, 5)</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// let arr = [1, 2, 3, 4, 5, 6]// 平方 等价于 arr.map(item =&gt; Math.pow(item, 2))arr.reduce((result, item) =&gt; &#123;return result.concat(item * item)&#125;, [])</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]arr.<span class="title function_">filter</span>(<span class="function"><span class="params">item</span> =&gt;</span> item %<span class="number">2</span> === <span class="number">0</span>)<span class="comment">// 等价于arr.reduce((result, item) =&gt; &#123;    if(item % 2 === 0)&#123;        return result    &#125;else&#123;        return result.concat(item)    &#125;&#125;, [])arr.reduce((result, item) =&gt; item % 2 === 1 ? result : result.concat(item), [])arr.reduce((result, item) =&gt; item % 2 === 1 ? result.concat([]) : result.concat(item), [])arr.reduce((result, item) =&gt; result.concat(item % 2 ? [] : item), [])</span></span><br></pre></td></tr></table></figure>



<p><strong>面试题: 数据变换</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [    &#123;名称: <span class="string">&#x27;动物&#x27;</span>, <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">parent</span>: <span class="literal">null</span>&#125;,    &#123;名称: <span class="string">&#x27;狗&#x27;</span>, <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">parent</span>: <span class="number">1</span>&#125;,    &#123;名称: <span class="string">&#x27;猫&#x27;</span>, <span class="attr">id</span>: <span class="number">3</span>, <span class="attr">parent</span>: <span class="number">1</span>&#125;]<span class="comment">// 数组变成对象&#123;    id: 1, 名称: &#x27;动物&#x27;, children: [        &#123;id: 2, 名称: &#x27;狗&#x27;, children: null&#125;,        &#123;id: 3, 名称: &#x27;猫&#x27;, children: null&#125;    ]&#125;// 答案arr.reduce((result, item) =&gt; &#123;    if(item.parent === null)&#123;        result.id = item.id        result[&#x27;名称&#x27;] = item[&#x27;item&#x27;]    &#125;else&#123;        result.children.push(item)        delete item.parent        item.chrldren = null    &#125;    return result&#125;, &#123;id: null, children: []&#125;)</span></span><br></pre></td></tr></table></figure>



<p><video src="C:\Users\34172\Desktop\Works\饥人谷\notes\JS\videos\面试题【数据变换】.mp4"></video></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>






      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/25/50/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/25/50/" class="post-title-link" itemprop="url">JS对象基本用法</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-08-25 21:41:50" itemprop="dateCreated datePublished" datetime="2021-08-25T21:41:50+08:00">2021-08-25</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h4 id="Object"><a href="#Object" class="headerlink" title="Object"></a>Object</h4><p>对象就是一组”键值对“（key-value）的集合，是一种<strong>无序</strong>的复合数据集合。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;frank&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> obj = <span class="keyword">new</span> <span class="title class_">Object</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;frank&#x27;</span>, </span><br><span class="line">    <span class="attr">age</span>: <span class="number">18</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(&#123;<span class="attr">name</span>: frank, <span class="attr">age</span>: <span class="number">18</span>&#125;)</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>键名都是字符串</strong>，不是标识符，可以包含任意字符（必须加上引号）</li>
<li>引号可以省略，省略后键名必须符合标识符的规则</li>
<li>所有键名都会被自动转成字符串</li>
</ul>
<p>特殊键名</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    <span class="number">1</span>: <span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">    <span class="number">3.2</span>: <span class="string">&#x27;b&#x27;</span>,</span><br><span class="line">    <span class="number">1e2</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="number">1e-2</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="number">.234</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="number">0xFF</span>: <span class="literal">true</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">keys</span>(obj) <span class="comment">// 列出 obj 所有的 key</span></span><br><span class="line">=&gt; [<span class="string">&quot;1&quot;</span>, <span class="string">&quot;100&quot;</span>, <span class="string">&quot;255&quot;</span>, <span class="string">&quot;3.2&quot;</span>, <span class="string">&quot;0.01&quot;</span>, <span class="string">&quot;0.234&quot;</span>]</span><br></pre></td></tr></table></figure>

<p>用变量做键名 用 <code>[ ]</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p1 = <span class="string">&#x27;name&#x27;</span>;</span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    <span class="attr">p1</span>: <span class="string">&#x27;frank&#x27;</span> <span class="comment">// 键名为 &#x27;p1&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">    [p1]: <span class="string">&#x27;frank&#x27;</span> <span class="comment">// 键名为 name</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 不加 [] 的键名会自动变成字符串</span></span><br><span class="line"><span class="comment">// 加了 [] 则会当作变量求值</span></span><br><span class="line"><span class="comment">// 值如果不是字符串，则会自动变成字符串</span></span><br><span class="line"><span class="keyword">var</span> obj &#123;</span><br><span class="line">    [<span class="number">1</span>+<span class="number">2</span>+<span class="number">3</span>]: <span class="string">&#x27;shi&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">keys</span>(obj);</span><br></pre></td></tr></table></figure>

<hr>


<h4 id="隐藏属性"><a href="#隐藏属性" class="headerlink" title="隐藏属性"></a>隐藏属性</h4><blockquote>
<ul>
<li>JS中每一个对象都一个隐藏属性 _<em>proto</em>_</li>
<li>这个隐藏属性存储着<strong>共有属性组成的对象</strong>的地址</li>
<li>这个共<strong>有属性组成的对象</strong>叫做原型</li>
<li>也就是说，隐藏属性存储着原型的的地址</li>
</ul>
</blockquote>
<p>示例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 隐藏属性为 __proto__</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123;&#125;</span><br><span class="line">obj.<span class="title function_">soString</span>() <span class="comment">// 不报错</span></span><br><span class="line">因为 obj 的隐藏属性 对应的对象 上有 <span class="title function_">toString</span>() </span><br></pre></td></tr></table></figure>

<hr>


<h4 id="删除属性"><a href="#删除属性" class="headerlink" title="删除属性"></a>删除属性</h4><p><strong>delete obj.xxx 或 delete obj[‘xxx’]</strong></p>
<blockquote>
<p>可删除 obj 的 xxx 属性</p>
<p>区分 【属性值为 undefined】和【不含属性名】</p>
</blockquote>
<p><strong>不含属性名</strong></p>
<blockquote>
<p>‘xxx’ in obj === false</p>
</blockquote>
<p><strong>含有属性名，但是值为 undefined</strong></p>
<blockquote>
<p>‘xxx’ in obj &amp;&amp; obj.xxx === undefined </p>
</blockquote>
<p><strong>注意 obj.xxx === undefined</strong></p>
<blockquote>
<p>不能断定 ‘xxx‘ 是否为 obj 的属性</p>
</blockquote>
<hr>


<h4 id="读取属性"><a href="#读取属性" class="headerlink" title="读取属性"></a>读取属性</h4><p><strong>查看属性</strong></p>
<blockquote>
<p>obj[‘key’]   </p>
<p>obj[key]</p>
<p>obj.key</p>
<p><strong>obj.name 等价于 obj[‘name’] 不等价于 obj[name]</strong>  这里的 name 是字符串，不是变量 </p>
</blockquote>
<p><strong>查看自身所有属性</strong></p>
<blockquote>
<p>Object.keys(obj);</p>
</blockquote>
<p><strong>查看自身 + 共有属性</strong></p>
<blockquote>
<p>console.dir(obj)</p>
<p>或者依次用 Object.keys 打印出 obj._<em>proto</em>_</p>
</blockquote>
<p><strong>判断一个属性是自身的还是共有</strong></p>
<blockquote>
<p>obj.hasOwnProperty(‘toString’)</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Object</span>.<span class="title function_">keys</span>(obj);</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">values</span>(obj);</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">entries</span>(obj);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">dir</span>(obj);</span><br></pre></td></tr></table></figure>

<p><strong>‘name’ in  obj 和 obj.hasOwnPrototy(‘name’) 区别</strong></p>
<blockquote>
<p>前者判断 obj 是否含有 name 属性名，但不会区分自身属性和共有属性</p>
<p>后者判断 obj 的属性 name 是自身的属性还是共有属性，返回 false，不是自身属性；发返回 ture，是自身属性。</p>
</blockquote>
<hr>
#### 原型

<p><strong>每个对象都有原型</strong></p>
<blockquote>
<p>原型里存着对象的共有属性</p>
<p>比如 obj 的原型就是一个对象</p>
<p>obj.__proto__存着这个对象的地址</p>
<p>这个对象里有 toString  / constructor / valueOf 等属性</p>
</blockquote>
<p><strong>对象的原型也是对象</strong></p>
<blockquote>
<p>所以对象的原型也有原型</p>
<p>obj = {} 的原型即为所有对象的原型</p>
<p>这个原型包含所有对象的共有属性，是<strong>对象的根</strong></p>
<p>这个原型也有原型，是 null。 console.log(_<em>proto</em><em>._<em>proto</em></em>)</p>
</blockquote>
<hr>

<h4 id="修改属性"><a href="#修改属性" class="headerlink" title="修改属性"></a>修改属性</h4><p><strong>直接赋值</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;<span class="attr">name</span>: <span class="string">&#x27;frank&#x27;</span>&#125; <span class="comment">// name 是字符串</span></span><br><span class="line">obj.<span class="property">name</span> = <span class="string">&#x27;frank&#x27;</span> <span class="comment">// name 是字符串</span></span><br><span class="line">obj[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;frank&#x27;</span></span><br><span class="line">❌obj[name] = <span class="string">&#x27;frank&#x27;</span> <span class="comment">// 错，因 name 不确定</span></span><br><span class="line">obj[<span class="string">&#x27;na&#x27;</span> + <span class="string">&#x27;me&#x27;</span>] = <span class="string">&#x27;frank&#x27;</span></span><br><span class="line"><span class="keyword">let</span> key = <span class="string">&#x27;name&#x27;</span>; obj[<span class="string">&#x27;key&#x27;</span>] = <span class="string">&#x27;frank&#x27;</span></span><br><span class="line"><span class="keyword">let</span> key = <span class="string">&#x27;name&#x27;</span>;  ❌ obj.<span class="property">key</span> = <span class="string">&#x27;frank&#x27;</span> <span class="comment">// 错</span></span><br><span class="line">因为 obj.<span class="property">key</span> 等价于 obj[<span class="string">&#x27;key&#x27;</span>]</span><br></pre></td></tr></table></figure>

<p><strong>批量赋值</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Object</span>.<span class="title function_">assign</span>(obj.&#123;<span class="attr">age</span>: <span class="number">18</span>, <span class="attr">gender</span>: <span class="string">&#x27;man&#x27;</span>&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="修改或增加共有属性"><a href="#修改或增加共有属性" class="headerlink" title="修改或增加共有属性"></a>修改或增加共有属性</h4><p><strong>无法通过自身修改或增加共有属性</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj1 = &#123;&#125;，obj2 = &#123;&#125;  <span class="comment">// 共有toString</span></span><br><span class="line">obj.<span class="property">toString</span> = <span class="string">&#x27;xxxx&#x27;</span> <span class="comment">// 只会改 obj1 自身的属性</span></span><br><span class="line">obj2.<span class="property">toString</span> 还是在原型上</span><br></pre></td></tr></table></figure>

<p><strong>偏要修改共有属性</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj1.<span class="property">__proto__</span>.<span class="property">toString</span> = <span class="string">&#x27;xxx&#x27;</span>; <span class="comment">// 不推荐使用__proto__Object.prototype.toString = &#x27;xxx&#x27;;一般不要修改原型</span></span><br></pre></td></tr></table></figure>

<h4 id="修改隐藏属性"><a href="#修改隐藏属性" class="headerlink" title="修改隐藏属性"></a>修改隐藏属性</h4><p><strong>不推荐使用_<em>proto</em></strong>_</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 代码示例</span></span><br><span class="line"><span class="keyword">let</span> person = &#123;<span class="attr">name</span>: <span class="string">&#x27;frank&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">let</span> person1 = &#123;<span class="attr">name</span>: <span class="string">&#x27;jack&#x27;</span>&#125;;</span><br><span class="line"><span class="keyword">let</span> common = &#123;<span class="attr">kind</span>: <span class="string">&#x27;human&#x27;</span>&#125;;</span><br><span class="line">obj.<span class="property">__proto__</span> = common;</span><br><span class="line">obj2.<span class="property">__proto__</span> = common;</span><br></pre></td></tr></table></figure>

<p><strong>强烈推荐使用 Object.create</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> persion = <span class="title class_">Object</span>.<span class="title function_">cteate</span>(common); <span class="comment">// 以 common 为原型创建 persion 对象</span></span><br><span class="line">persion.<span class="property">name</span> = <span class="string">&#x27;frank&#x27;</span>; <span class="comment">// 然后添加属性，或批量添加</span></span><br><span class="line"><span class="keyword">let</span> persion2 = <span class="title class_">Object</span>.<span class="title function_">create</span>(common, &#123; <span class="comment">// </span></span><br><span class="line">    <span class="attr">name</span>: &#123;<span class="attr">value</span>: <span class="string">&#x27;jack&#x27;</span>&#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<hr>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/24/11/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/24/11/" class="post-title-link" itemprop="url">JS基本语法</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-08-24 14:05:11" itemprop="dateCreated datePublished" datetime="2021-08-24T14:05:11+08:00">2021-08-24</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h4 id="表达式与语句"><a href="#表达式与语句" class="headerlink" title="表达式与语句"></a>表达式与语句</h4><p>表达式（expression），指一个为了得到返回值的计算式。</p>
<blockquote>
<p>1 + 2 表达式的<strong>值</strong>为3</p>
<p>add(1 + 2) 表达式的值为函数的返回值</p>
<p>console.log 表达式的值为函数本身</p>
<p>console.log(3) 表达式的值为<strong>undefined</strong></p>
</blockquote>
<p>语句（statement），是为了完成某种任务而进行的操作，例如赋值语句<code>var a = 1 + 3;</code></p>
<blockquote>
<p>var a = 1 是一个语句</p>
<p>var a = 1 + 3 </p>
<p>var b = ‘abc’</p>
</blockquote>
<p>二者区别</p>
<blockquote>
<p>表达式一般都有值，语句可能有也可能没有</p>
<p>语句一般不会改变环境（声明、赋值）</p>
<p>以上并不绝对</p>
<p>语句主要为了进行某种操作，一般情况下不需要返回值；表达式则是为了得到返回值，一定会返回一个值。凡是 JavaScript 语言中预期为值的地方，都可以使用表达式。比如，赋值语句的等号右边，预期是一个值，因此可以放置各种表达式。</p>
<p>表达式不需要分号结尾。一旦在表达式后面添加分号，则 JavaScript 引擎就将表达式视为语句，这样会产生一些没有任何意义的语句。</p>
</blockquote>
<hr>

<h4 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h4><p>指的是用来识别各种值的合法名称，最常见的标识符就是变量名，以及函数名。</p>
<p>JavaScript 语言的标识符对大小写敏感，所以<code>a</code>和<code>A</code>是两个不同的标识符。</p>
<p>命名规则：</p>
<ul>
<li>第一个字符，可以是任意 Unicode 字母（包括英文字母和其他语言的字母），以及美元符号（<code>$</code>）和下划线（<code>_</code>）。</li>
<li>第二个字符及后面的字符，除了 Unicode 字母、美元符号和下划线，还可以用数字<code>0-9</code>。</li>
</ul>
<blockquote>
<p>JavaScript  有一些保留字，不能用作标识符：arguments、break、case、catch、class、const、continue、debugger、default、delete、do、else、enum、eval、export、extends、false、finally、for、function、if、implements、import、in、instanceof、interface、let、new、null、package、private、protected、public、return、static、super、switch、this、throw、true、try、typeof、var、void、while、with、yield。</p>
</blockquote>
<hr>



<h4 id="if-else"><a href="#if-else" class="headerlink" title="if else"></a>if else</h4><p><code>if</code>结构先判断一个表达式的布尔值（true or false），然后根据布尔值的真伪，执行不同的语句。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (表达式) &#123;</span><br><span class="line">  <span class="comment">// 语句</span></span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span> (表达式)&#123;</span><br><span class="line">    <span class="comment">// 语句</span></span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span> (表达式)&#123;</span><br><span class="line">    <span class="comment">// 语句</span></span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<hr>

<h4 id="switch"><a href="#switch" class="headerlink" title="switch"></a>switch</h4><p>多个 <code>if ...else</code> 连在一起使用的时候，可以转为是使用 <code>switch</code> 结构。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (fruit) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="string">&quot;banana&quot;</span>:</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line">  <span class="keyword">case</span> <span class="string">&quot;apple&quot;</span>:</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line">  <span class="attr">default</span>:<span class="string">&quot;nothing&quot;</span></span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// switch 语句部分和 case 语句部分，都可以使用表达式。</span></span><br></pre></td></tr></table></figure>



<hr>

<h4 id="三元运算符"><a href="#三元运算符" class="headerlink" title="三元运算符"></a>三元运算符</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">(条件) ？ 表达式<span class="number">1</span> : 表达式<span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> even = (n % <span class="number">2</span> === <span class="number">0</span>) ? <span class="literal">true</span> : <span class="literal">false</span>;</span><br><span class="line"><span class="comment">// 如果n可以被2整除，则even等于true，否则等于false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> msg = <span class="string">&#x27;数字&#x27;</span> + n + <span class="string">&#x27;是&#x27;</span> + (n % <span class="number">2</span> === <span class="number">0</span> ? <span class="string">&#x27;偶数&#x27;</span> : <span class="string">&#x27;奇数&#x27;</span>);</span><br></pre></td></tr></table></figure>



<hr>

<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><p>逻辑与运算（&amp;&amp;）是 AND 布尔操作。只有两个操作数都为 true 时，才返回 true，否则返回 false。具体描述如表所示。</p>
<table>
<thead>
<tr>
<th>第一个操作数</th>
<th>第二个操作数</th>
<th>运算结果</th>
</tr>
</thead>
<tbody><tr>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
</tbody></table>
<p> 逻辑与是一种短路逻辑，如果左侧表达式为 false，则直接短路返回结果，不再运算右侧表达式。运算逻辑如下：</p>
<ul>
<li>第 1 步：计算第一个操作数（左侧表达式）的值。</li>
<li>第 2 步：检测第一个操作数的值。如果左侧表达式的值可转换为 false（如 null、undefined、NaN、0、””、false），那么就会结束运算，<strong>直接返回第一个操作数的值</strong>。</li>
<li>第 3 步：如果第一个操作数可以转换为 true，则计算第二个操作数（右侧表达式）的值。</li>
<li>第 4 步：返回第二个操作数的值。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> (&#123;&#125; &amp;&amp; <span class="literal">true</span>));  <span class="comment">//返回第二个操作数的值  true的类型：布尔型</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> (<span class="literal">true</span> &amp;&amp; &#123;&#125;));  <span class="comment">//返回第二个操作数的值  &#123;&#125;的类型：对象</span></span><br><span class="line"><span class="variable language_">console</span> &amp;&amp; <span class="variable language_">console</span>.<span class="property">log</span> &amp;&amp; <span class="variable language_">console</span>.<span class="title function_">log</span>(hi) <span class="comment">// 返回 hi</span></span><br><span class="line">fn &amp;&amp; <span class="title function_">fn</span>() <span class="comment">// 常见写法 如果fn存在调用fn()</span></span><br></pre></td></tr></table></figure>

<hr>
**辑或运**算`||`是布尔 OR 操作。如果两个操作数都为 true，或者其中一个为 true，就返回 true，否则就返回 false。


<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = a || <span class="number">100</span></span><br><span class="line"><span class="comment">// 等价于</span></span><br><span class="line"><span class="keyword">if</span> (a) &#123;</span><br><span class="line">    a = a</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    a = <span class="number">100</span> <span class="comment">// 保底值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<hr>


<h4 id="while"><a href="#while" class="headerlink" title="while"></a>while</h4><p><code>While</code> 语句包括一个循环条件和一段代码块，只要条件为真，就不断循环执行代码块。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (条件) &#123;</span><br><span class="line">  语句;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 例 死循环</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">0.1</span>;</span><br><span class="line"><span class="keyword">while</span> (a !== <span class="number">1</span>) &#123;</span><br><span class="line">    cconsole.<span class="title function_">log</span>(a);</span><br><span class="line">    a = a + <span class="number">0.1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 因为浮点数不精确</span></span><br></pre></td></tr></table></figure>



<hr>


<h4 id="for"><a href="#for" class="headerlink" title="for"></a>for</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (初始化表达式; 条件; 递增表达式) &#123;</span><br><span class="line">    语句</span><br><span class="line">&#125;</span><br><span class="line">先执行 初始化表达式</span><br><span class="line">然后判断 条件</span><br><span class="line">如果为 真，执行循环体，然后执行 递增表达式</span><br><span class="line">如果为 假，直接退出循环。</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 例</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i&lt;<span class="number">5</span>; i++) &#123;</span><br><span class="line">	<span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(i + <span class="string">&#x27;随机数：&#x27;</span> + <span class="title class_">Math</span>.<span class="title function_">random</span>());</span><br><span class="line">    &#125;, <span class="number">0</span>);</span><br><span class="line">&#125;<span class="comment">// 输出5个5</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/wl0804/p/11987833.html">参考</a></p>
<hr>


<h4 id="for-in"><a href="#for-in" class="headerlink" title="for in"></a>for in</h4><p>for/in 语句是 for 语句的一种特殊形式。<a target="_blank" rel="noopener" href="http://c.biancheng.net/view/5527.html">参考</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> ( [<span class="keyword">var</span>] variable <span class="keyword">in</span> &lt;object | array)&#123;</span><br><span class="line">    statement</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* variable 表示一个变量，可以在其前面附加 var 语句，用来直接声明变量名。in 后面是一个对象或数组类型的表达式。在遍历对象或数组过程中，把或取的每一个值赋值给 variable。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">然后执行 statement 语句，其中可以访问 variable 来读取每个对象属性或数组元素的值。执行完毕，返回继续枚举下一个元素，以此类推知道所有元素都被枚举为止。</span></span><br><span class="line"><span class="comment">对于数组来说，值是数组元素的下标；对于对象来说，值是对象的属性名或方法名。*/</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用 for/in 语句遍历数组，并枚举每个元素及其值。</span></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="literal">true</span>,<span class="string">&quot;0&quot;</span>,[<span class="literal">false</span>],&#123;&#125;];  </span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> n <span class="keyword">in</span> a) &#123;  </span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">write</span>(<span class="string">&quot;a[&quot;</span>+n+<span class="string">&quot;] = &quot;</span> + a[n] + <span class="string">&quot;&lt;br&gt;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<hr>


<h4 id="break-和-continue"><a href="#break-和-continue" class="headerlink" title="break 和 continue"></a>break 和 continue</h4><p>break 语句能够结束当前 for、for/in、while、do/while 或者 switch语句的执行。</p>
<p>continue 语句用在循环结构内，用于跳过本次循环，继续执行下一次循环。</p>
<hr>


<h4 id="label"><a href="#label" class="headerlink" title="label"></a>label</h4><p>标签</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">foo</span>: &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">break</span> foo;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;本行不会输出&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// m 一个标签，内容为1</span></span><br><span class="line">&#123;<span class="attr">foo</span>:<span class="number">1</span>&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>参考自<a target="_blank" rel="noopener" href="https://wangdoc.com/javascript/basic/grammar.html#%E8%AF%AD%E5%8F%A5">网道 JavaScript教程</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/22/39/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/22/39/" class="post-title-link" itemprop="url">JavaScript的诞生</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-08-22 21:11:39" itemprop="dateCreated datePublished" datetime="2021-08-22T21:11:39+08:00">2021-08-22</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="JavaScript的历史"><a href="#JavaScript的历史" class="headerlink" title="JavaScript的历史"></a>JavaScript的历史</h2><p>JavaScript（简称“JS”）  是一种具有函数优先的轻量级，解释型或即时编译型的编程语言。虽然它是作为开发 Web 页面的脚本语言而出名的，但是它也被用到了很多非浏览器环境中，JavaScript 基于原型编程、多范式的动态脚本语言，并且支持面向对象、命令式和声明式（如函数式编程）风格。</p>
<p>1995年由于网景公司需要做出一款比 Java 还要简单的脚本语言，但是还要于Java相似。这时刚入职没多久的 <strong>Brendan Eich</strong> 就被安排了这项任务。</p>
<p><strong>1995年5月</strong>，<strong>Brendan Eich</strong>只用了10天，就设计完成了这种语言的第一版。</p>
<center><img src="https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/w%3D249/sign=5801118fad51f3dec3b2be60adeef0ec/838ba61ea8d3fd1f84237b7f324e251f95ca5f49.jpg"></center>

<p>1996年11月，网景正式向 ECMA（欧洲计算机制造商协会）提交语言标准。1997年6月，ECMA以JavaScript语言为基础制定了ECMAScript 标准规范ECMA-262。JavaScript成为了ECMAScript最著名的实现之一。除此之外，ActionScript 和 JScript 也都是ECMAScript 规范的实现语言。尽管JavaScript作为给非程序人员的脚本语言，而非作为给程序人员的脚本语言来推广和宣传，但是 JavaScript 具有非常丰富的特性。</p>
<h2 id="JavaScrip-特点"><a href="#JavaScrip-特点" class="headerlink" title="JavaScrip 特点"></a>JavaScrip 特点</h2><p>一般来说，完整的 JavaScript 包括以下几个部分:</p>
<ul>
<li>ECMAScript，描述了该语言的语法和基本对象</li>
<li>文档对象模型 DOM，描述处理<strong>网页内容</strong>的方法和接口</li>
<li>浏览器对象模型 BOM 浏览器对象模型，描述与<strong>浏览器</strong>进行交互的方法和接口</li>
</ul>
<p>JavaScript的基本特点如下:</p>
<ul>
<li>是一种解释性脚本语言（代码不进行预编译）。</li>
<li>主要用来向HTML页面添加<strong>交互行为</strong>。</li>
<li>可以直接嵌入HTML页面，但写成单独的js文件有利于结构和行为的分离。</li>
</ul>
<p>JavaScript常用来完成以下任务:</p>
<ul>
<li>嵌入动态文本于HTML页面</li>
<li>对浏览器事件作出响应</li>
<li>读写HTML元素</li>
<li>在数据被提交到服务器之前验证数据</li>
<li>检测访客的浏览器信息</li>
<li>控制 cookie，包括创建和修改等</li>
</ul>
<p>JavaScript 语法有多个来源：</p>
<ul>
<li>基本语法：借鉴 C语言和 Java 语言。</li>
<li>数据机构：借鉴 Java语言，包括将值分为原始值和对象两大类。</li>
<li>函数的用发：借鉴 Scheme语言 （Smalltalk的一种变种）。</li>
<li>正则表达式：借鉴 Perl语言。</li>
<li>字符串和数组处理：借鉴 Python语言。</li>
</ul>
<p>为了保持简单，这种脚本语言缺少一些关键的功能，比如块级作用域、模块、子类型（subtyping）等等，但是可以利用现有功能找出解决办法。这种功能的不足，直接导致了后来JavaScript的一个显著特点：对于其他语言，你需要学习语言的各种功能，而对于JavaScript，你常常需要学习各种解决问题的模式。而且由于来源多样，从一开始就注定，JavaScript的编程风格是函数式编程和面向对象编程的一种混合体。</p>
<h2 id="JavaScript的缺陷"><a href="#JavaScript的缺陷" class="headerlink" title="JavaScript的缺陷"></a>JavaScript的缺陷</h2><ol>
<li><p><strong>不适合开发大型程序</strong></p>
<blockquote>
<p>Javascript没有名称空间（namespace），很难模块化；没有如何将代码分布在多个文件的规范；允许同名函数的重复定义，后面的定义可以覆盖前面的定义，很不利于模块化加载。</p>
</blockquote>
</li>
<li><p><strong>非常小的标准库</strong></p>
<blockquote>
<p>Javascript提供的标准函数库非常小，只能完成一些基本操作，很多功能都不具备。</p>
</blockquote>
</li>
<li><p><strong>null 和 undefined</strong></p>
<blockquote>
<p>null属于对象（object）的一种，意思是该对象为空；undefined则是一种数据类型，表示未定义。</p>
</blockquote>
</li>
<li><p><strong>全局变量难以控制</strong></p>
<blockquote>
<p>Javascript的全局变量，在所有模块中都是可见的；任何一个函数内部都可以生成全局变量，这大大加剧了程序的复杂性。</p>
</blockquote>
</li>
<li><p><strong>自动插入行尾分号</strong></p>
<blockquote>
<p>Javascript的所有语句，都必须以分号结尾。但是，如果你忘记加分号，解释器并不报错，而是为你自动加上分号。有时候，这会导致一些难以发现的错误。</p>
</blockquote>
</li>
<li><p><strong>加号运算符</strong></p>
<blockquote>
<p>+号作为运算符，有两个含义，可以表示数字与数字的和，也可以表示字符与字符的连接。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">alert</span>(<span class="number">1</span>+<span class="number">10</span>); <span class="comment">// 11</span></span><br><span class="line"><span class="title function_">alert</span>(<span class="string">&quot;1&quot;</span>+<span class="string">&quot;10&quot;</span>); <span class="comment">// 110</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>如果一个操作项是字符，另一个是数字，则数字自动转化为字符</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">alert</span>(<span class="number">1</span>+<span class="string">&quot;10&quot;</span>) <span class="comment">// 110</span></span><br><span class="line"><span class="title function_">alert</span>(<span class="string">&quot;10&quot;</span>+<span class="number">1</span>) <span class="comment">// 101</span></span><br></pre></td></tr></table></figure></li>
<li><p><strong>NaN</strong></p>
<blockquote>
<p>NaN是一种数字，表示超出了解释器的极限。</p>
</blockquote>
</li>
<li><p><strong>数组和对象的区分</strong></p>
<blockquote>
<p>由于Javascript的数组也属于对象（object），所以要区分一个对象到底是不是数组，相当麻烦</p>
</blockquote>
</li>
<li><p><strong>== 和 ===</strong></p>
<blockquote>
<p>==用来判断两个值是否相等。当两个值类型不同时，会发生自动转换，得到的结果非常不符合直觉。</p>
<p>推荐任何时候都是用 “===”</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;</span> == <span class="string">&quot;0&quot;</span>  <span class="comment">// false</span></span><br><span class="line"><span class="number">0</span> == <span class="string">&quot;&quot;</span>  <span class="comment">// true</span></span><br><span class="line"><span class="number">0</span> == <span class="string">&quot;0&quot;</span>  <span class="comment">// frue</span></span><br><span class="line"><span class="literal">false</span> == <span class="string">&quot;false&quot;</span>  <span class="comment">// false</span></span><br><span class="line"><span class="literal">false</span> == <span class="literal">undefined</span>  <span class="comment">// false</span></span><br><span class="line"><span class="literal">false</span> == <span class="literal">null</span>  <span class="comment">// false</span></span><br><span class="line"><span class="literal">null</span> == <span class="literal">undefined</span>  <span class="comment">// true</span></span><br><span class="line"><span class="string">&quot; \t\r\n&quot;</span> == <span class="number">0</span>  <span class="comment">// ture</span></span><br></pre></td></tr></table></figure></li>
<li><p><strong>基本类型的包装对象</strong></p>
<blockquote>
<p>Javascript有三种基本数据类型：字符串、数字和布尔值。它们都有相应的建构函数，可以生成字符串对象、数字对象和布尔值对象。</p>
</blockquote>
</li>
</ol>
<p>资料连接：</p>
<p><a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/JavaScript#%E5%8E%86%E5%8F%B2">JavaScript 的历史</a></p>
<p><a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/2011/06/birth_of_javascript.html">JavaScript 诞生记</a></p>
<p><a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/2011/06/10_design_defects_in_javascript.html">JavaScript 的10个设计缺陷</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/10/41/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/10/41/" class="post-title-link" itemprop="url">浅析URL</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-10 19:47:41" itemprop="dateCreated datePublished" datetime="2021-07-10T19:47:41+08:00">2021-07-10</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>万维网（World Wide Web）包含三个部分分别是：</p>
<p><strong>WWW=URL（Uniform）+HTTP（HyperText Transfer Protocol）+HTML（HyperText Markup Language）</strong></p>
<p>URL= 协议+域名或IP+端口号+路径+查询字符串+锚点</p>
<p><strong><a href="https://link.zhihu.com/?target=http://www.baidu.com/s?wd=hi&rsv_spt=1%235">http://www.baidu.com/s?wd=hi&amp;rsv_spt=1#5</a></strong></p>
<blockquote>
<p><strong>协议 —— <a href="https://link.zhihu.com/?target=http://zhi/">http://</a></strong><br><strong>域名 —— <a href="https://link.zhihu.com/?target=http://www.baidu.com/s?wd=hi&rsv_spt=1%235">www.baidu.com</a></strong><br><strong>路径 —— <a href="https://link.zhihu.com/?target=http://www.baidu.com/s?wd=hi&rsv_spt=1%235">/s</a></strong><br><strong>查询参数 —— <a href="https://link.zhihu.com/?target=http://www.baidu.com/s?wd=hi&rsv_spt=1%235">?wd=hi&amp;rsv_spt=1</a></strong><br><strong>锚点 —— <a href="https://link.zhihu.com/?target=http://www.baidu.com/s?wd=hi&rsv_spt=1%235">#5</a></strong></p>
</blockquote>
<h2 id="协议"><a href="#协议" class="headerlink" title="协议"></a><strong>协议</strong></h2><p>服务器使用的协议一般有两种：</p>
<blockquote>
<p>一种为<strong>HTTP（</strong>HyperText Transfer Protocol,超文本传输协议)<br>一种为<strong>HTTPS（</strong>Hyper Text Transfer Protocol over SecureSocket Layer,超文本传输安全协议）</p>
</blockquote>
<hr>
<h2 id="IP（Internet-Protocol）"><a href="#IP（Internet-Protocol）" class="headerlink" title="IP（Internet Protocol）"></a>IP（Internet Protocol）</h2><p><strong>作用</strong></p>
<blockquote>
<p> 一、如何定位一台设备（手机、电脑、路由器…）<br> 二、如何封装数据报文，以跟其他设备交流</p>
</blockquote>
<p>换句话说，只要你在互联网中，你就至少会有一个独立的IP</p>
<p> <strong>IP分为内网和外网</strong></p>
<h2 id="外网IP"><a href="#外网IP" class="headerlink" title="外网IP"></a><strong>外网IP</strong></h2><p><strong>如何获取外网的IP（过程）</strong></p>
<p>第一步：从电信租用带宽</p>
<p>第二步：光猫（调制解调器）</p>
<blockquote>
<p>作用：拨号上网</p>
</blockquote>
<p>第三步：路由器</p>
<blockquote>
<p>作用：起到分配的作用</p>
</blockquote>
<p>把电脑和手机分别连接由路由器广播出来的无线WIFI</p>
<p>只要路由器通过“光猫”连接上外面（电信）的服务器时，路由器就会有一个「外网IP」，比如「147.17.32.211」就是一个外网IP，这个IP地址就是在互联网中的地址（所有的通过路由器连接设备都是同一个外网IP地址）</p>
<p>但是如果重启路由器，那么很有可能被重新分配一个「外网IP」，换句话说你的路由器没有「固定的外网IP」</p>
<p>拓展：<a target="_blank" rel="noopener" href="https://www.ip138.com/">查询自己的外网IP</a></p>
<h2 id="内网IP"><a href="#内网IP" class="headerlink" title="内网IP"></a>内网IP</h2><p>路由器会在家里（区域）创建一个内网，内网中的设备使用时内网IP，一般来说内网IP格式为「<a href="https://link.zhihu.com/?target=http://192.168.xxx.xxx">http://192.168.xxx.xxx</a>」</p>
<p>一般路由器会给自己分配一个好记的内网IP：192.168.1.1（一般会让出一个位置留给光猫）</p>
<p>然后路由器会给每一个内网中的设备分配一个不同的内网IP，如电脑是192.168.1.2，手机是192.168.1.3，以此类推。</p>
<h2 id="路由器"><a href="#路由器" class="headerlink" title="路由器"></a>路由器</h2><p>路由器有两个IP：一个外网IP和一个内网IP</p>
<p>内网中的设备可以互相访问，但是不能直接访问外网</p>
<p>内网设备想要访问外网，就必须经过路由器中转</p>
<p>外网中的设备可以互相访问，但无法直接访问内网设备</p>
<p>外网设备想要把内容送到内网，也必须通过路由器</p>
<p>换句换说内网和外网就像两个隔绝的空间无法互通，唯一的联通点就是路由器</p>
<p>所以路由器有时候也被叫做网关</p>
<h2 id="特殊IP"><a href="#特殊IP" class="headerlink" title="特殊IP"></a><strong>特殊IP</strong></h2><blockquote>
<p>127.0.0.1 本地</p>
<p>localhost 本地</p>
<p>0.0.0.0（不表示任何设备）</p>
</blockquote>
<h2 id="域名"><a href="#域名" class="headerlink" title="域名"></a><strong>域名</strong></h2><blockquote>
<p> 域名就是IP的别称</p>
</blockquote>
<p><strong>知识：</strong></p>
<ul>
<li><strong>一个域名可以对应不同的IP</strong></li>
<li><strong>这个叫做均衡负载，防止一台机器扛不住</strong></li>
</ul>
<blockquote>
<p>终端输入ping <a href="https://link.zhihu.com/?target=http://baidu.com/">baidu.com</a> 会生成几个IP地址来对应不同的服务器，这些服务器分别对应各个地区（华中、华南、华北..），进行使用</p>
</blockquote>
<ul>
<li><strong>一个IP可以对应不同域名</strong></li>
<li><strong>这个叫做共享主机</strong></li>
</ul>
<blockquote>
<p>公司太小公用服务器或者一个公司有两个网站，公用一个IP地址</p>
</blockquote>
<p><strong>疑问 ️：<a href="https://link.zhihu.com/?target=http://www.baidu/">www.</a>xiedaimala.com与xiedaimala.com是同一个域名吗？</strong></p>
<blockquote>
<p>答案：不是同一个域名</p>
</blockquote>
<p><strong>拓展：</strong></p>
<blockquote>
<p>com（company，公司）<strong>是顶级域名</strong><br><a href="https://link.zhihu.com/?target=http://xiedaimala.com">http://xiedaimala.com</a>是<strong>二级域名（俗称一级域名）</strong><br><a href="https://link.zhihu.com/?target=http://www.xiedaimala.com">http://www.xiedaimala.com</a>是<strong>三级域名（俗称二级域名）</strong></p>
</blockquote>
<p> <strong>他们是父子关系（越长越小）</strong> </p>
<blockquote>
<p><a href="https://link.zhihu.com/?target=http://github.io/">github.io</a> 把子域名xxx.github.io免费给用户使用 xx.com与<a target="_blank" rel="noopener" href="http://www.xx.com可以是同一家公司,也可以不是同一家公司(只是公用父域名)/">www.xx.com可以是同一家公司，也可以不是同一家公司（只是公用父域名）</a> www是非常多余的！</p>
</blockquote>
<h2 id="端口"><a href="#端口" class="headerlink" title="端口"></a>端口</h2><p><strong>什么是端口</strong></p>
<blockquote>
<p>我们知道URL的作用是定位服务器比如定位百度服务器），我们可以通过上面的IP/域名来定位服务器，但是我们定位服务器的根本目的是需要为服务的（不然我们定位服务器干啥！！ ）<br>所有现在接下来的事情就是定位服务，而<strong>端口</strong>就是为了对应不同服务的</p>
</blockquote>
<p><strong>端口号</strong></p>
<blockquote>
<p>一台服务器（设备）可以提供很多服务，每个服务都有对应号码，这个号码被称为<strong>端口号</strong></p>
</blockquote>
<p>很多服务</p>
<p>要提供HTTP（超文本传输协议）服务最好使用80端口</p>
<p>要提供HTTPS（超文本传输安全协议）服务最好使用443端口</p>
<p>要提供FTP（文件传输协议）服务最好使用443端口</p>
<p>一共有65535个端口，基本够用</p>
<p>我们怎么知道什么服务对应（提供）什么端口</p>
<hr>
<h2 id="路径"><a href="#路径" class="headerlink" title="路径"></a><strong>路径</strong></h2><blockquote>
<p>在一个服务器（设备）上，如何请求不同的页面 通过路径可以做到</p>
</blockquote>
<blockquote>
<p>例：</p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTML">https://developer.mozilla.org/zh-CN/docs/Web/HTML</a></p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/css">https://developer.mozilla.org/zh-CN/docs/Web/css</a></p>
</blockquote>
<h2 id="查询参数"><a href="#查询参数" class="headerlink" title="查询参数"></a><strong>查询参数</strong></h2><p>同一页面，不同内容</p>
<p><a target="_blank" rel="noopener" href="https://www.baidu.com/s?wd=hi&amp;pn=10">https://www.baidu.com/s?wd=hi&amp;pn=10</a></p>
<p><a target="_blank" rel="noopener" href="https://www.baidu.com/s?wd=hello">https://www.baidu.com/s?wd=hello</a></p>
<blockquote>
<p><strong>/s：搜索页面</strong><br><strong>?wd=：查询参数 word（单词/词语）</strong><br><strong>wd：word（单词/词语）</strong></p>
</blockquote>
<h2 id="锚点"><a href="#锚点" class="headerlink" title="锚点"></a><strong>锚点</strong></h2><blockquote>
<p>在一个内容上，如何请求不同位置 通过锚点可以做到</p>
</blockquote>
<p>举 ：</p>
<blockquote>
<p><a href="https://link.zhihu.com/?target=https://develop.mozilla.org/zh-CN/docs/web/HTML">https://develop.mozilla.org/zh-CN/docs/web/HTML</a><strong>#参考书</strong> <a href="https://link.zhihu.com/?target=https://develop.mozilla.org/zh-CN/docs/web/CSS">https://develop.mozilla.org/zh-CN/docs/web/CSS</a><strong>#教程</strong></p>
</blockquote>
<p><strong>注意：</strong></p>
<blockquote>
<p>锚点看起来有中文，实际不支持中文 <strong>#参考书</strong>传输到服务器会变成一堆乱码 锚点无法在Network面板看到 因为锚点不会传給服务器</p>
</blockquote>
<h2 id="DNS"><a href="#DNS" class="headerlink" title="DNS"></a>DNS</h2><blockquote>
<p><strong>DNS全称为Domain Name System，域名系统</strong></p>
</blockquote>
<blockquote>
<p>DNS 的作用就是对域名和IP进行相互对应的</p>
</blockquote>
<p><strong>验证下：当我们输入<a href="https://link.zhihu.com/?target=http://baidu.com">http://baidu.com</a>回车后，域名是如何与IP进行相互对应的？</strong></p>
<ul>
<li>在Chrome浏览器下输入<a href="https://link.zhihu.com/?target=http://baidu.com">http://baidu.com</a>后，先经过<strong>路由器</strong>向外网的<strong>（电信/联通提供的DNS服务器）</strong>发出询问，询问内容就是<a href="https://link.zhihu.com/?target=http://baidu.com">http://baidu.com</a>所对应的IP地址</li>
<li><strong>电信/联通提供的DNS服务器</strong>收到请求后，就会回答一个IP地址</li>
<li>然后Chrome浏览器把回答的IP地址和以及IP对应的端口（80/443端口）再次通过路由器向对应的服务器发送请求</li>
<li>请求内容：查看<a href="https://link.zhihu.com/?target=http://baidu.com">http://baidu.com</a>的页面</li>
<li>服务器通过路由器再传回给浏览器（HTML、CSS、JS等）</li>
<li>最终我们就可以看到<a href="https://link.zhihu.com/?target=http://baidu.com">http://baidu.com</a>的页面了</li>
</ul>
<p><strong>通过nslookup 命令查看域名下所有提供服务的服务器</strong></p>
<blockquote>
<p>语法：nslookup 网站域名</p>
</blockquote>
<p><strong>拓展：</strong>ping与nslookup区别：</p>
<blockquote>
<p>ping：查看域名<strong>对应</strong>本地区的服务器，更为准确 nslookup：查看域名对应<strong>所有</strong>提供服务的服务器</p>
</blockquote>
<h2 id="curl命令"><a href="#curl命令" class="headerlink" title="curl命令"></a>curl命令</h2><p>用 curl 可以发 HTTP 请求</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">curl -v http://baidu.com</span><br><span class="line">curl -s -v -- https://www.baidu.com</span><br></pre></td></tr></table></figure>

<p>理解：</p>
<blockquote>
<p>url 会被 curl 工具重写，先请求 DNS 获得 IP</p>
<p>先进行 TCP 连接，TCP 连接成功后，开始发送 HTTP　请求</p>
<p>请求内容看一眼</p>
<p>响应内容看一眼</p>
<p>响应结束后，关闭 TCP 连接（看不出来）</p>
<p>真正结束</p>
</blockquote>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/10/31/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/10/31/" class="post-title-link" itemprop="url">css总结</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-10 18:11:31" itemprop="dateCreated datePublished" datetime="2021-07-10T18:11:31+08:00">2021-07-10</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="选择器"><a href="#选择器" class="headerlink" title="选择器"></a>选择器</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>.群组选择器  如：<span class="selector-tag">p</span>, <span class="selector-tag">body</span>, <span class="selector-tag">img</span>, <span class="selector-tag">div</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>.兄弟选择器  如：<span class="selector-tag">p</span> + <span class="selector-tag">p</span> &#123; <span class="attribute">color</span>:<span class="number">#f00</span>; &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3</span>.属性选择器  如： <span class="selector-tag">p</span><span class="selector-attr">[title]</span> &#123; <span class="attribute">color</span>:<span class="number">#f00</span>; &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">4</span>.包含（后代）选择器  如：<span class="selector-tag">body</span> <span class="selector-tag">ul</span> <span class="selector-tag">li</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5</span>.子元素选择器 如：<span class="selector-tag">div</span> &gt; <span class="selector-tag">p</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6</span><span class="selector-class">.ID</span>选择器  如：<span class="selector-id">#myDiv</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7</span>.类选择器  如：<span class="selector-class">.class1</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">8</span>.伪元素选择器  如：E<span class="selector-pseudo">:first</span>-line，E<span class="selector-pseudo">:before</span></span><br><span class="line"></span><br><span class="line"><span class="number">9</span>.伪类选择器  如：E<span class="selector-pseudo">:first</span>-child ，E<span class="selector-pseudo">:visited</span>，E<span class="selector-pseudo">:focus</span>，E<span class="selector-pseudo">:enabled</span></span><br><span class="line"></span><br><span class="line"><span class="number">10</span>.标签选择器  如：<span class="selector-tag">p</span> &#123; <span class="attribute">font-size</span>:<span class="number">1em</span>; &#125;</span><br><span class="line"></span><br><span class="line">优先级： !important &gt; 行内样式 &gt; ID选择器 &gt; class选择器 &gt; 标签 &gt; 通配符 &gt; 继承 </span><br></pre></td></tr></table></figure>



<h2 id="定位（position）"><a href="#定位（position）" class="headerlink" title="定位（position）"></a>定位（position）</h2><ol>
<li>static，默认，待在文档流里</li>
<li>relative，相对定位，原位置不变，显示的位置偏移<ul>
<li>用于做位移（很少用）</li>
<li>用于给 absolute 元素做父元素</li>
<li>配合 z-index，z-index :auto 默认值，不创建新层叠上下文</li>
</ul>
</li>
<li>absolute，绝对定位，定位基准是祖先里的非static即相对于祖先元素中最近的一个定位元素<ul>
<li>脱离原来的位置，另起一层，<a target="_blank" rel="noopener" href="http://js.jirengu.com/hosugodegi/3/edit">比如对话框的关闭按钮</a>，祖先元素得加 relative </li>
<li>某些浏览器不写 top / left 会位置错乱。善用 left: 100% left: 50% 加负 margin</li>
</ul>
</li>
<li>fixed，固定定位，定位基准是 viewport，视口代表当前可见的计算机图形区域，transform 有bug<ul>
<li>手机上尽量不要使用 fixed</li>
</ul>
</li>
<li>sticky，粘滞定位，兼容差</li>
</ol>
<p>Tips：</p>
<ul>
<li><p>如果写了 absolute，一般都得补一个 relative</p>
</li>
<li><p>如果写了 absolute 或 fixed，一定要补 top 和 left</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/position">position MDN</a></p>
<h2 id="盒模型"><a href="#盒模型" class="headerlink" title="盒模型"></a>盒模型</h2><center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/20180324150509906.jpg" alt="20180324150509906" width="800px"></center>



<ol>
<li>content-box 内容就是盒子的边界<ul>
<li>content-box width = 内容宽度</li>
</ul>
</li>
<li>border-box 边框才是盒子的边界 （推荐使用）<ul>
<li>border-box width = 内容宽度 + padding + border</li>
</ul>
</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">box-sizing</span>: border-box | content-box;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/The_box_model">CSS 基础框盒模型介绍  MDN</a></p>
<h2 id="float-布局"><a href="#float-布局" class="headerlink" title="float 布局"></a>float 布局</h2><p>步骤：</p>
<ol>
<li>子元素加上 float: left 和 width</li>
<li>在父元素上加 .clearfix</li>
</ol>
<p>经验:</p>
<ul>
<li>有经验者会留一些空间或者最后有一个不设 width</li>
<li>不需要响应式，因为手机上没有 IE，而这个布局是专门为 IE准备的</li>
</ul>
<p>IE 6/7 存在双倍 margin bug, 两个解决办法（基本不用）：</p>
<ol>
<li>针对 IE 6/7 把 margin 减半</li>
<li>加一个 display: inline-block </li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.clearfix</span><span class="selector-pseudo">:after</span> &#123;</span><br><span class="line">    <span class="comment">/* 清除浮动 */</span></span><br><span class="line">    <span class="attribute">content</span>: <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="attribute">display</span>: block;</span><br><span class="line">    <span class="attribute">clear</span>: both;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">IE bug</span><br><span class="line"><span class="comment">/* margin 减半 &amp;&amp; inline-block */</span></span><br><span class="line"><span class="selector-class">.class</span> &#123;</span><br><span class="line">    <span class="attribute">margin-left</span>: <span class="number">10px</span>;</span><br><span class="line">    _margin-<span class="attribute">left</span>: <span class="number">5px</span>;</span><br><span class="line">    <span class="attribute">display</span>: inline-block;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.php.cn/css-tutorial-412744.html">为什么要清除浮动</a></p>
<p><a target="_blank" rel="noopener" href="http://js.jirengu.com/wiwuh/3/edit?html,css,output">float demo</a></p>
<p>Tips：</p>
<ul>
<li>border 有时候会干扰尺寸，可以改成 outline</li>
<li>用 float 做平均布局时，可以在布局中增加一个x图层，其多余的右边距可以用 <strong>负的 margin</strong> 来改善布局</li>
</ul>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_08-33-25.png" alt="Snipaste_2021-08-20_08-33-25" width="800px"></center>



<h2 id="浏览器渲染原理"><a href="#浏览器渲染原理" class="headerlink" title="浏览器渲染原理"></a>浏览器渲染原理</h2><ul>
<li>根据 HTML 构建 HTML 树（DOM）</li>
<li>根据 CSS 构建 CSS 树 （CSS DOM）</li>
<li>将两棵树合并成一颗渲染树 （render tree）</li>
<li>Layout 布局（文档流、盒模型、计算大小和位置）</li>
<li>Paint 绘制 （把边框颜色、文字颜色、阴影等画出来）</li>
<li>Compose 合成（根据层叠关系展示画面）</li>
</ul>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/render-tree-construction.png" alt="render-tree-construction" width="800px"></center>

<p><a target="_blank" rel="noopener" href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction">渲染树构建、布局及绘制</a></p>
<h2 id="动画"><a href="#动画" class="headerlink" title="动画"></a>动画</h2><p><strong>transition</strong></p>
<p>css中的transition允许css的属性值在一定的时间区间内平滑地过渡。这种效果可以在鼠标单击、获得焦点、被点击或对元素任何改变中触发，并圆滑的以动画效果改变css的属性值。</p>
<p>transition属性是个复合属性 <code>transition: property name | duration | timing function | delay;</code>，包括以下几个子属性：</p>
<ul>
<li>transition-property ：规定设置过渡效果的css属性名称<ul>
<li><code>transition-property: none |all |property</code></li>
</ul>
</li>
<li>transition-duration ：规定完成过渡效果需要多少秒或毫秒<ul>
<li><code>transition-duration: time</code></li>
</ul>
</li>
<li>transition-timing-function ：指定过渡函数，规定速度效果的速度曲线<ul>
<li><code>transition-timing-function：linear| ease| ease-in| ease-out| ease-in-out| cubic-bezier(n,n,n,n);</code></li>
</ul>
</li>
<li>transition-delay ：指定开始出现的延迟时间<ul>
<li><code>transition-delay: time</code></li>
</ul>
</li>
</ul>
<p><strong>animation</strong></p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/animation">animation</a></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">animation</span>: 时长|过渡方式|延迟|次数|方向|填充模式|是否暂停|动画名；</span><br><span class="line"></span><br><span class="line">方向：reverse | alternate | alternate—reverse</span><br><span class="line">填充模式：forwards | none | backwards | both</span><br><span class="line">暂停：paused | running</span><br></pre></td></tr></table></figure>



<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes">keyframes</a>语法格式</p>
<p><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/12991164/maintaining-the-final-state-at-end-of-a-css3-animation/12991203#12991203">让动画停在最后一帧</a>  加forwards ，例 <code>animation: xxx 1.5s forwards</code>;</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/10/50/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/10/50/" class="post-title-link" itemprop="url">css定位与层叠上下文</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-10 16:35:50" itemprop="dateCreated datePublished" datetime="2021-07-10T16:35:50+08:00">2021-07-10</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="一个-div-的分层"><a href="#一个-div-的分层" class="headerlink" title="一个 div 的分层"></a>一个 div 的分层</h2><center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-07-12_21-36-28.png" alt="Snipaste_2021-07-12_21-36-28" width="800px"></center>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-07-12_22-04-00.png" alt="Snipaste_2021-07-12_22-04-00" width="800px"></center>



<h2 id="position"><a href="#position" class="headerlink" title="position"></a>position</h2><ol>
<li>static，默认，待在文档流里</li>
<li>relative，相对定位，原位置不变，显示的位置偏移<ul>
<li>用于做位移（很少用）</li>
<li>用于给 absolute 元素做父元素</li>
<li>配合 z-index，z-index :auto 默认值，不创建新层叠上下文</li>
</ul>
</li>
<li>absolute，绝对定位，定位基准是祖先里的非static即相对于祖先元素中最近的一个定位元素<ul>
<li>脱离原来的位置，另起一层，<a target="_blank" rel="noopener" href="http://js.jirengu.com/hosugodegi/3/edit">比如对话框的关闭按钮</a>，祖先元素得加 relative </li>
<li>某些浏览器不写 top / left 会位置错乱。善用 left: 100% left: 50% 加负 margin</li>
</ul>
</li>
<li>fixed，固定定位，定位基准是 viewport，视口代表当前可见的计算机图形区域，transform 有bug<ul>
<li>手机上尽量不要使用 fixed</li>
</ul>
</li>
<li>sticky，粘滞定位，兼容差</li>
</ol>
<p>Tips：</p>
<p>如果写了 absolute，一般都得补一个 relative</p>
<p>如果写了 absolute 或 fixed，一定要补 top 和 left</p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/position">position MDN</a></p>
<h2 id="层叠上下文"><a href="#层叠上下文" class="headerlink" title="层叠上下文"></a>层叠上下文</h2><center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-07-13_21-47-35.png" alt="Snipaste_2021-07-13_21-47-35" width="800px"></center>

<p>比喻：</p>
<ul>
<li>每个层叠上下文既是一个小世界（作用域）</li>
<li>这个小世界里面的 z-index 跟外界无关</li>
<li>处在同一个小世界的 z-index 才能比较</li>
<li>负 z-index 逃不出小世界，<a target="_blank" rel="noopener" href="http://js.jirengu.com/modez/1/edit?html,css,output">例</a></li>
</ul>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context">那些属性可以创建，查mdn</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/10/08/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/10/08/" class="post-title-link" itemprop="url">浏览器渲染原理</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-10 16:29:08" itemprop="dateCreated datePublished" datetime="2021-07-10T16:29:08+08:00">2021-07-10</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><strong>浏览器渲染原理</strong></p>
<p>步骤：</p>
<ul>
<li>根据 HTML 构建 HTML 树（DOM）</li>
<li>根据 CSS 构建 CSS 树 （CSS DOM）</li>
<li>将两棵树合并成一颗渲染树 （render tree）</li>
<li>Layout 布局（文档流、盒模型、计算大小和位置）</li>
<li>Paint 绘制 （把边框颜色、文字颜色、阴影等画出来）</li>
<li>Compose 合成（根据层叠关系展示画面）</li>
</ul>
<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/render-tree-construction.png" alt="render-tree-construction" width="800px"></center>



<p><strong>JS三种更新样式方式</strong></p>
<ol>
<li><p>JS/CSS &gt; 样式 &gt; 布局 &gt; 绘制 &gt; 合成</p>
<img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/frame-full.jpg" alt="frame-full" width="800px">

<p>如果修改元素的“layout”属性，也就是改变了元素的几何属性（例如宽度、高度、左侧或顶部位置等），那么浏览器将必须检查所有其他元素，然后“自动重排”页面。任何受影响的部分都需要重新绘制，而且最终绘制的元素需进行合成。</p>
</li>
<li><p>JS/CSS &gt; 样式 &gt; 绘制 &gt; 合成</p>
<img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/frame-no-layout.jpg" alt="frame-no-layout" width="800px">

<p>如果修改“paint only”属性（例如背景图片、文字颜色或阴影等），即不会影响页面布局的属性，则浏览器会跳过布局，但仍将执行绘制。</p>
</li>
<li><p>JS/CSS &gt; 样式 &gt; 合成</p>
<img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/frame-no-layout-paint.jpg" alt="frame-no-layout-paint" width="800px">

<p>如果更改一个既不要布局也不要绘制的属性，则浏览器将跳到只执行合成。</p>
<p>对于应用程序生命周期中的高压点（例如动画或滚动），此最终版本是开销最小和最理想的。</p>
<p>This final version is the cheapest and most desirable for high pressure points in an app’s lifecycle, like animations or scrolling.</p>
</li>
</ol>
<p>例：勾选 Paint flashing查看</p>
<ol>
<li><a target="_blank" rel="noopener" href="http://js.jirengu.com/jagel/1/edit?html,css,js,output">div.remove()</a> 会触发当前消失，其他元素 relayout ，再 repaint + composte</li>
<li><a target="_blank" rel="noopener" href="http://js.jirengu.com/jidam/1/edit?html,css,js,output">change bg color</a> 直接 repaint + composite</li>
<li><a target="_blank" rel="noopener" href="http://js.jirengu.com/wusew/1">change transform</a> 只需 composite</li>
</ol>
<p>查看css属性变更时触发的改变 <a target="_blank" rel="noopener" href="https://csstriggers.com/">CSSTrigers</a></p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-tree-construction">渲染树构建、布局及绘制</a></p>
<p><a target="_blank" rel="noopener" href="https://developers.google.com/web/fundamentals/performance/rendering/">渲染性能</a></p>
<p><a target="_blank" rel="noopener" href="https://developers.google.com/web/fundamentals/performance/rendering/stick-to-compositor-only-properties-and-manage-layer-count">使用 transform 实现动画</a></p>
</blockquote>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/09/53/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/09/53/" class="post-title-link" itemprop="url">css基础</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-09 16:02:53" itemprop="dateCreated datePublished" datetime="2021-07-09T16:02:53+08:00">2021-07-09</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="文档流"><a href="#文档流" class="headerlink" title="文档流"></a>文档流</h2><ol>
<li>流动方向<ul>
<li>inline 从左到右，到达最右边才会换行</li>
<li>block 从上到下，每一个都另起一行</li>
<li>inline-block 从左到右</li>
</ul>
</li>
<li>宽度<ul>
<li>inline 宽度为内部 inline 元素的和，不能用 width 指定</li>
<li>block 默认自动计算宽度 auto ，可用 width 指定</li>
<li>inline-block 结合前两者特点，可用 width</li>
</ul>
</li>
<li>高度<ul>
<li>inline 高度由行高 line-height 间接确定，跟 height 无关</li>
<li>block 高度由文档流元素决定，可设 height</li>
<li>inline-block 跟 block 类似，可设置 height</li>
</ul>
</li>
</ol>
<h2 id="overflow-溢出"><a href="#overflow-溢出" class="headerlink" title="overflow 溢出"></a>overflow 溢出</h2><p>当内容大于容器</p>
<ul>
<li>等内容的款年度或高度大于容器的，会溢出</li>
<li>可用 overflow 来设置是否显示滚动条</li>
<li>auto 是灵活设置</li>
<li>scroll 是永远设置</li>
<li>hidden 是直接隐藏移除部分</li>
<li>visible 是直接显示溢出部分</li>
<li>overflow 可分为 overflow-x 和 overflow-y 很少用</li>
</ul>
<h2 id="脱离文档流"><a href="#脱离文档流" class="headerlink" title="脱离文档流"></a>脱离文档流</h2><p>block 高度由内部文档流元素决定，可以设 height，这句话的意思是不是说，有些元素可以不在文档流中</p>
<ul>
<li>position: absolute/fixed;</li>
<li>float</li>
</ul>
<hr>


<h2 id="盒模型"><a href="#盒模型" class="headerlink" title="盒模型"></a>盒模型</h2><ol>
<li>content-box 内容就是盒子的边界<ul>
<li>content-box width = 内容宽度</li>
</ul>
</li>
<li>border-box 边框才是盒子的边界 （推荐使用）<ul>
<li>border-box width = 内容宽度 + padding + border</li>
</ul>
</li>
</ol>
<h2 id="margin-合并"><a href="#margin-合并" class="headerlink" title="margin 合并"></a>margin 合并</h2><ul>
<li>父子。兄弟之间会合并，只有上下合并</li>
<li>阻止合并：<ul>
<li>父子合并用 padding / border  挡住</li>
<li>父子合并用 overflow：hidden 挡住</li>
<li>父子合并用 display: flex</li>
<li>兄弟合并是符合预期的，可以用 display: inline-block 消除</li>
</ul>
</li>
</ul>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/09/28/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/09/28/" class="post-title-link" itemprop="url">grid布局</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-07-09 15:55:28" itemprop="dateCreated datePublished" datetime="2021-07-09T15:55:28+08:00">2021-07-09</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>采用网格布局的区域，称为“容器（container）”。容器内部的采用网格定位的子元素称为“项目（item）”</p>
<h2 id="container"><a href="#container" class="headerlink" title="container"></a>container</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">    <span class="comment">/* 指定一个容器为 */</span> </span><br><span class="line">   <span class="attribute">display</span>: grid | inline-grid;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="columns-row"><a href="#columns-row" class="headerlink" title="columns row"></a>columns row</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">    <span class="attribute">grid-template-columns</span>: <span class="number">40px</span> <span class="number">50px</span> auto <span class="number">50px</span> <span class="number">40px</span>;</span><br><span class="line">    <span class="attribute">grid-template-rows</span>: <span class="number">25%</span> <span class="number">100px</span> auto;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-07-10_23-02-04.png" alt="Snipaste_2021-07-10_23-02-04" width="600px"></center>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">grid-template-columns</span>: <span class="built_in">repeat</span>(<span class="number">4</span>,<span class="number">1</span>fr); // 宽度平均分成四等份 </span><br><span class="line"><span class="attribute">grid-template-columns</span>: <span class="number">1</span>fr <span class="number">2</span>fr <span class="number">3</span>fr;   // 列宽这样是分成<span class="number">6</span>份各占 <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> 份</span><br></pre></td></tr></table></figure>



<h2 id="grid-template-areas"><a href="#grid-template-areas" class="headerlink" title="grid-template-areas"></a>grid-template-areas</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.item-a</span> &#123;</span><br><span class="line">  <span class="attribute">grid-area</span>: header;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.item-b</span> &#123;</span><br><span class="line">  <span class="attribute">grid-area</span>: main;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.item-c</span> &#123;</span><br><span class="line">  <span class="attribute">grid-area</span>: sidebar;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.item-d</span> &#123;</span><br><span class="line">  <span class="attribute">grid-area</span>: footer;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">display</span>: grid;</span><br><span class="line">  <span class="attribute">grid-template-columns</span>: <span class="number">50px</span> <span class="number">50px</span> <span class="number">50px</span> <span class="number">50px</span>;</span><br><span class="line">  <span class="attribute">grid-template-rows</span>: auto;</span><br><span class="line">  <span class="attribute">grid-template-areas</span>: </span><br><span class="line">    <span class="string">&quot;header header header header&quot;</span></span><br><span class="line">    <span class="string">&quot;main main . sidebar&quot;</span></span><br><span class="line">    <span class="string">&quot;footer footer footer footer&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_12-33-29.png" alt="Snipaste_2021-08-20_12-33-29" width="600px"></center>



<h2 id="gap"><a href="#gap" class="headerlink" title="gap"></a>gap</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">grid-template-columns</span>: <span class="number">100px</span> <span class="number">50px</span> <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">grid-template-rows</span>: <span class="number">80px</span> auto <span class="number">80px</span>; </span><br><span class="line">  <span class="attribute">column-gap</span>: <span class="number">10px</span>;</span><br><span class="line">  <span class="attribute">row-gap</span>: <span class="number">15px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">    <span class="attribute">grid-gap</span>: &lt;grid-row-gap&gt; &lt;grid-column-gap&gt;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_13-02-30.png" alt="Snipaste_2021-08-20_13-02-30" width="600px"></center>



<h2 id="item"><a href="#item" class="headerlink" title="item"></a>item</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.item-a</span> &#123;</span><br><span class="line">  <span class="attribute">grid-column-start</span>: <span class="number">2</span>;</span><br><span class="line">  <span class="attribute">grid-column-end</span>: five;</span><br><span class="line">  <span class="attribute">grid-row-start</span>: row1-start;</span><br><span class="line">  <span class="attribute">grid-row-end</span>: <span class="number">3</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_13-18-20.png" alt="Snipaste_2021-08-20_13-18-20" width="600px"></center>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.item-b</span> &#123;</span><br><span class="line">  <span class="attribute">grid-column-start</span>: <span class="number">1</span>;</span><br><span class="line">  <span class="attribute">grid-column-end</span>: span col4-start;</span><br><span class="line">  <span class="attribute">grid-row-start</span>: <span class="number">2</span>;</span><br><span class="line">  <span class="attribute">grid-row-end</span>: span <span class="number">2</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_13-33-02.png" alt="Snipaste_2021-08-20_13-33-02" width="600px"></center>



<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.item-c</span> &#123;</span><br><span class="line">  <span class="attribute">grid-column</span>: <span class="number">3</span> / span <span class="number">2</span>;</span><br><span class="line">  <span class="attribute">grid-row</span>: third-line / <span class="number">4</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2021-08-20_13-23-08.png" alt="Snipaste_2021-08-20_13-23-08" width="600px"></center>



<p><a target="_blank" rel="noopener" href="https://cssgridgarden.com/#zh-cn">grid 游戏</a></p>
<p>参照 <a target="_blank" rel="noopener" href="https://css-tricks.com/snippets/css/complete-guide-grid/#introduction">A Complete Guide to Grid</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/3/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/page/5/">5</a><a class="extend next" rel="next" href="/page/5/"><i class="fa fa-angle-right" aria-label="Next page"></i></a>
  </nav>

</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
